/**
 * Created by x on 17-8-27.
 */
angular.module('tribalc').controller('addWelfareController',['$scope',
    '$rootScope',
    'Api',
    'Alert',
    '$state',
    'welfareModel',
    function ($scope,$rootScope,Api,Alert,$state,welfareModel) {
        $scope.welfareModels = new welfareModel()
        $scope.goBackWelfare = goBackWelfare;
        $scope.doThis= doThis;
        $scope.doNoSelect = doNoSelect;
        $scope.releaseWelfare = releaseWelfare;
        $scope.getFocusInformation = getFocusInformation;
        $scope.leaveFocusInformation = leaveFocusInformation;
        $scope.changeSelectClass = changeSelectClass;
        $scope.getSex = getSex;
        $scope.getChossInformation = getChossInformation;
        $scope.selectList = []

        $scope.getInformation = getInformation;
        $scope.selected = true;
        $scope.aselected = true;
        $scope.exchanged = true;
        $scope.exchangedback = true;
        $scope.showEnterpassword = false;
        $scope.exchangeBack = exchangeBack;
        $scope.exchange = exchange;
        $scope.staffList = [];
        $scope.welfareReason = "";
        $scope.welfarePrice = "";

        $scope.selectList = [];

        getWelfareStaffList();
        function getWelfareStaffList() {
            Api.getWelfareStaffList(null,null,null,function (result) {
                $scope.staffList = result.data;
            })
        }

        function goBackWelfare() {
            $state.go('company.companyWelfare')
        }
        function clone (data){
            return JSON.parse(JSON.stringify(data))
        }
        $rootScope.selectLists =[];
        function exchange() {
            if (!$scope.welfareModels.totalFee)
            {
                Alert.alertBox("请输入人均金额");
                return;
            }
            //if($('.select-img-box').find('img').attr('src') === 'img/company/033.png'){
                $scope.exchanged = false;
               for(var i =0;i<$scope.rightSelectList.length; i++){
                   $rootScope.selectLists.push(clone($scope.rightSelectList[i]));

               }
               for(var i =0; i<$scope.staffList.length;i++){
                   var status = false;
                   for(var j =0; j<$scope.rightSelectList.length;j++){
                       if($scope.staffList[i].name == $scope.rightSelectList[j].name){
                           status = true;
                           break;
                       }
                   }
                   if(status){
                       $scope.staffList.splice(i,1);
                       i =-1;
                       console.log($scope.staffList)
                   }

               }
            $scope.rightSelectList = []
            $scope.aselected = true;
            calculateToyalPrice()

        }

        function exchangeBack() {
            if (!$scope.welfareModels.totalFee)
            {
                Alert.alertBox("请输入人均金额");
                return;
            }

            //if($('#back').find('img').attr('src') === 'img/company/023.png'){
                $scope.exchangedback = false;
                for(var i =0;i<$scope.leftSelectList.length; i++){
                    $scope.staffList.push(clone($scope.leftSelectList[i]));

                }
                for(var i =0; i<$scope.selectLists.length;i++){
                    var status = false;
                    for(var j =0; j<$scope.leftSelectList.length;j++){
                        if($scope.selectLists[i].name == $scope.leftSelectList[j].name){
                            status = true;
                            break;
                        }
                    }
                    if(status){
                        $scope.selectLists.splice(i,1);
                        i =-1;
                    }

                }
            $scope.leftSelectList = []
            $scope.selected = true;

            calculateToyalPrice();
        }
        
        function calculateToyalPrice() {
            $scope.total = $scope.selectLists.length * $scope.welfareModels.totalFee;
        }

        function getIndex(array, element,key){
            var status = -1;
            for(var i =0; i<array.length;i++){
                    if(array[i][key] == element[key]){
                        status = i;
                        break;
                    }
            }

            return status
        }



        function doThis() {
            var Checkall = document.getElementById('choseAll');
            var div = document.getElementById('chose');
            var CheckBox=div.getElementsByTagName('li');
            if($('.chosen-title').find('.img').attr('src') === 'img/765.png'){
                $scope.selected = false;
                $('#chose').find('li').addClass('staff-triangl-select-img');
                for(var i=0;i<CheckBox.length;i++){
                    CheckBox[i].checked=true;
                    $scope.leftSelectList = clone($scope.selectLists);
                }
            }else{
                $scope.selected = true;
                $('#chose').find('li').removeClass('staff-triangl-select-img');
                for(var i=0;i<CheckBox.length;i++){
                    CheckBox[i].checked=false;
                }
            }
        }

        function doNoSelect() {
            $scope.rightSelectList = [];
            var Checkall = document.getElementById('All');
            var div = document.getElementById('div');
            var CheckBox=div.getElementsByTagName('li');
            if($('.nochosen-title').find('.img').attr('src') === 'img/765.png'){
                $scope.aselected = false;
                $('#div').find('li').addClass('staff-triangl-select-img')
                for(var i=0;i<CheckBox.length;i++){
                    CheckBox[i].checked=true;
                    $scope.rightSelectList = clone($scope.staffList)
                }
            }else{
                $scope.aselected = true;
                $('#div').find('li').removeClass('staff-triangl-select-img')
                for(var i=0;i<CheckBox.length;i++){
                    CheckBox[i].checked=false;
                }
            }
        }

        function releaseWelfare(welfare) {
            if (!welfare.note)
            {
                Alert.alertBox("请输入发放理由");
                return;
            }
            if (!welfare.totalFee || welfare.totalFee==0)
            {
                Alert.alertBox("请输入人均金额或人均金额不能为0");
                return;
            }
            if ($scope.selectLists.length == 0)
            {
                Alert.alertBox("请选择需要发放福利的员工");
                return;
            }
            $scope.showEnterpassword = true;
            $rootScope.totalFee = welfare.totalFee;
            $rootScope.note = welfare.note;
        }
        $scope.rightSelectList = []
        function getInformation(welfareinfo,index) {
            $scope.selected = welfareinfo
            if ($('#div').find('li').eq(index).hasClass('staff-triangl-select-img')) {
                $('#div').find('li').eq(index).removeClass('staff-triangl-select-img')
                var elementIndex = getIndex($scope.rightSelectList, welfareinfo, 'name');
                if(elementIndex>-1){
                    $scope.rightSelectList.splice(elementIndex, 1)
                }
            } else{
                $('#div').find('li').eq(index).addClass('staff-triangl-select-img');
                $scope.rightSelectList.unshift(clone(welfareinfo))
            }


        }

        $scope.leftSelectList = []
        function getChossInformation(welfareinfo,index) {
            $scope.selected = welfareinfo
            if ($('#chose').find('li').eq(index).hasClass('staff-triangl-select-img')) {
                $('#chose').find('li').eq(index).removeClass('staff-triangl-select-img')

                var elementIndex = getIndex($scope.leftSelectList, welfareinfo, 'name');
                if(elementIndex>-1){
                    $scope.leftSelectList.splice(elementIndex, 1)
                }

            } else {
                $('#chose').find('li').eq(index).addClass('staff-triangl-select-img')
                $scope.leftSelectList.push(clone(welfareinfo))
            }
        }

        function getFocusInformation(welfareinfo) {
            $scope.hoverWelfare = welfareinfo;
        }

        function leaveFocusInformation(welfareinfo) {
            $scope.hoverWelfare = !welfareinfo;
        }

        function changeSelectClass(welfareinfo) {
            if (welfareinfo == $scope.hoverWelfare) {
                return 'staff-triangl-hover-img';
            } else if($scope.selected=='balck'){
                return '';
            }
            else {
                return '';
            }
        }

        function getSex(sex) {
            return {'MALE':'男','FEMALE':'女','null':'未知'}[sex]
        }

    }])
